package com.mybed.crm.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.codec.Base64;
import cn.hutool.http.server.HttpServerResponse;
import com.mybed.crm.anno.Log;
import com.mybed.crm.mapper.VerificationMapper;
import com.mybed.crm.pojo.pojo.Verification;
import com.mybed.crm.pojo.vo.UserInfoVo;
import com.mybed.crm.result.ImgResult;
import com.mybed.crm.result.ProfileResult;
import com.mybed.crm.result.Result;
import com.mybed.crm.service.MenuService;
import com.mybed.crm.service.UserService;
import com.mybed.crm.utils.alioss.AliOssUtil;
import com.wf.captcha.SpecCaptcha;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping
@Slf4j
public class CommonController {
    @Autowired
    private UserService userService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private MenuService menuService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private VerificationMapper verificationMapper;
    //@ApiOperation("获取路由信息")
    @GetMapping("getRouters")
    public String getRouters()
    {
        return "{\"msg\":\"操作成功\",\"code\":200,\"data\":[{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"首页\",\"icon\":\"icon_menu_home\",\"noCache\":false},\"children\":[{\"name\":\"Index\",\"path\":\"index\",\"hidden\":false,\"component\":\"indexHome\",\"meta\":{\"title\":\"首页\",\"icon\":\"icon_menu_home\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"线索管理\",\"icon\":\"icon_menu_xsgl\",\"noCache\":false},\"children\":[{\"name\":\"Clue\",\"path\":\"clue\",\"hidden\":false,\"component\":\"clues/clue/index\",\"meta\":{\"title\":\"线索管理\",\"icon\":\"icon_menu_xsgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"商机管理\",\"icon\":\"icon_menu_sjgl\",\"noCache\":false},\"children\":[{\"name\":\"Business\",\"path\":\"business\",\"hidden\":false,\"component\":\"clues/business/index\",\"meta\":{\"title\":\"商机管理\",\"icon\":\"icon_menu_sjgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"合同管理\",\"icon\":\"icon_menu_htgl\",\"noCache\":false},\"children\":[{\"name\":\"Contract\",\"path\":\"contract\",\"hidden\":false,\"component\":\"clues/contract/index\",\"meta\":{\"title\":\"合同管理\",\"icon\":\"icon_menu_htgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"活动管理\",\"icon\":\"icon_menu_hdgl\",\"noCache\":false},\"children\":[{\"name\":\"Activity\",\"path\":\"activity\",\"hidden\":false,\"component\":\"clues/activity/index\",\"meta\":{\"title\":\"活动管理\",\"icon\":\"icon_menu_hdgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"课程管理\",\"icon\":\"icon_menu_kcgl\",\"noCache\":false},\"children\":[{\"name\":\"Course\",\"path\":\"course\",\"hidden\":false,\"component\":\"clues/course/index\",\"meta\":{\"title\":\"课程管理\",\"icon\":\"icon_menu_kcgl\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"统计分析\",\"icon\":\"icon_menu_tjfx\",\"noCache\":false},\"children\":[{\"name\":\"CountAnalysis\",\"path\":\"countAnalysis\",\"hidden\":false,\"component\":\"clues/countAnalysis/index\",\"meta\":{\"title\":\"统计分析\",\"icon\":\"icon_menu_tjfx\",\"noCache\":false}}]},{\"path\":\"/\",\"hidden\":false,\"component\":\"Layout\",\"meta\":{\"title\":\"转派管理\",\"icon\":\"example\",\"noCache\":false},\"children\":[{\"name\":\"TransferManage\",\"path\":\"transferManage\",\"hidden\":false,\"component\":\"clues/transferManage/index\",\"meta\":{\"title\":\"转派管理\",\"icon\":\"example\",\"noCache\":false}}]},{\"name\":\"System\",\"path\":\"/system\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"Layout\",\"alwaysShow\":true,\"meta\":{\"title\":\"系统管理\",\"icon\":\"icon_menu_xtgl\",\"noCache\":false},\"children\":[{\"name\":\"Permission\",\"path\":\"permission\",\"hidden\":false,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"权限管理\",\"icon\":\"icon_menu_qxgl\",\"noCache\":false},\"children\":[{\"name\":\"User\",\"path\":\"user\",\"hidden\":false,\"component\":\"system/user/index\",\"meta\":{\"title\":\"用户管理\",\"icon\":\"user\",\"noCache\":false}},{\"name\":\"Role\",\"path\":\"role\",\"hidden\":false,\"component\":\"system/role/index\",\"meta\":{\"title\":\"角色管理\",\"icon\":\"peoples\",\"noCache\":false}},{\"name\":\"Menu\",\"path\":\"menu\",\"hidden\":false,\"component\":\"system/menu/index\",\"meta\":{\"title\":\"菜单管理\",\"icon\":\"tree-table\",\"noCache\":false}},{\"name\":\"Dept\",\"path\":\"dept\",\"hidden\":false,\"component\":\"system/dept/index\",\"meta\":{\"title\":\"部门管理\",\"icon\":\"tree\",\"noCache\":false}},{\"name\":\"Post\",\"path\":\"post\",\"hidden\":false,\"component\":\"system/post/index\",\"meta\":{\"title\":\"岗位管理\",\"icon\":\"post\",\"noCache\":false}}]},{\"name\":\"Dict\",\"path\":\"dict\",\"hidden\":false,\"component\":\"system/dict/index\",\"meta\":{\"title\":\"字典管理\",\"icon\":\"icon_menu_zdgl\",\"noCache\":false}},{\"name\":\"Clew\",\"path\":\"clew\",\"hidden\":false,\"component\":\"system/clew/index\",\"meta\":{\"title\":\"线索配置\",\"icon\":\"icon_menu_xspz\",\"noCache\":false}},{\"name\":\"Opportunity\",\"path\":\"opportunity\",\"hidden\":false,\"component\":\"system/opportunity/index\",\"meta\":{\"title\":\"商机配置\",\"icon\":\"icon_menu_sjpz\",\"noCache\":false}},{\"name\":\"NoticeCenter\",\"path\":\"noticeCenter\",\"hidden\":false,\"component\":\"system/noticeCenter/index\",\"meta\":{\"title\":\"通知中心\",\"icon\":\"icon_menu_tzzx\",\"noCache\":false}},{\"name\":\"Log\",\"path\":\"log\",\"hidden\":true,\"redirect\":\"noRedirect\",\"component\":\"ParentView\",\"alwaysShow\":true,\"meta\":{\"title\":\"日志管理\",\"icon\":\"log\",\"noCache\":false},\"children\":[{\"name\":\"Operlog\",\"path\":\"operlog\",\"hidden\":false,\"component\":\"monitor/operlog/index\",\"meta\":{\"title\":\"操作日志\",\"icon\":\"form\",\"noCache\":false}},{\"name\":\"Logininfor\",\"path\":\"logininfor\",\"hidden\":false,\"component\":\"monitor/logininfor/index\",\"meta\":{\"title\":\"登录日志\",\"icon\":\"logininfor\",\"noCache\":false}}]},{\"name\":\"SystemLog\",\"path\":\"systemLog\",\"hidden\":false,\"component\":\"system/systemLog/index\",\"meta\":{\"title\":\"系统日志\",\"icon\":\"icon_menu_xtrz\",\"noCache\":false}}]}]}";
    }
    /*@GetMapping("getRouters")
    public Result getRouters()
    {
        return menuService.getRouters();
    }*/
    @GetMapping("/getInfo")
    public UserInfoVo getInfo(){
        UserInfoVo userInfoVo=userService.getInfo();
        userInfoVo.setMsg("操作成功");
        userInfoVo.setCode(200);
        return userInfoVo;
    }

    /**
     * 获取图形验证码
     * @return
     */
    @GetMapping("/captchaImage")
    public ImgResult captchaImage(){
        /*LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(50, 20);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        lineCaptcha.write(byteArrayOutputStream);
        String base64Image = Base64.encode(byteArrayOutputStream.toByteArray());*/
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
        String base64Image = specCaptcha.toBase64("");
        String uuid = UUID.randomUUID().toString();
        String clientIp = request.getHeader("X-Real-IP");
        if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getHeader("X-Forwarded-For");
        }
        if (clientIp == null || clientIp.isEmpty() || "unknown".equalsIgnoreCase(clientIp)) {
            clientIp = request.getRemoteAddr();
        }
        log.info("登入的Ip,{}",clientIp);
        Verification v=new Verification();
        v.setTime(System.currentTimeMillis());
        v.setCode(specCaptcha.text().toString());
        v.setUuid(uuid);
        v.setIpAddr(clientIp);
        verificationMapper.insert(v);
        ImgResult result=new ImgResult();
        result.insertData(base64Image,uuid);

        return result;
    }

    /**
     * 文件上传
     * @param file
     * @return
     */
    //@Log("文件上传")
    @PostMapping("/common/upload")
    public ProfileResult<String> uploadPdf(MultipartFile file){
        log.info("文件上传:{}",file);

        try {
            String originFilename = file.getOriginalFilename();
            String extension = originFilename.substring(originFilename.lastIndexOf('.'));
            String objectName = UUID.randomUUID().toString() + extension;
            String filePath = aliOssUtil.upload(file.getBytes(),objectName);
            String prefix = "https://";
            if (filePath.startsWith(prefix)) {
                filePath = filePath.substring(prefix.length());
            }
            return ProfileResult.success(filePath);
        } catch (IOException e) {
            log.error("文件上传失败:{}",e);
        }

        return null;
    }
}
